package com.dz.utils;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.dz.ejb.session.FacadeUserInterface;
import com.dz.ejb.session.FacadeZombieInterface;

public final class Session {
	
	public static void createFacade(){
		createFacadeUser();
		createFacadeZombie();
	}
	
	private static void createFacadeUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		FacadeUserInterface facadeUser = (FacadeUserInterface) session.getAttribute(Constants.FACADE_USER);
	    if (facadeUser == null) {
            Context ctx;
            try {
                ctx = new InitialContext();
                facadeUser = (FacadeUserInterface) ctx.lookup("java:module/FacadeUser!com.dz.ejb.session.FacadeUserInterface");
                session.setAttribute(Constants.FACADE_USER, facadeUser);
            } catch (NamingException e) {
                e.printStackTrace();
            }
	    }
	}
	
	private static void createFacadeZombie(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		FacadeZombieInterface facadeZombie = (FacadeZombieInterface) session.getAttribute(Constants.FACADE_ZOMBIE);
	    if (facadeZombie == null) {
            Context ctx;
            try {
                ctx = new InitialContext();
                facadeZombie = (FacadeZombieInterface) ctx.lookup("java:module/FacadeZombie!com.dz.ejb.session.FacadeZombieInterface");
                session.setAttribute(Constants.FACADE_ZOMBIE, facadeZombie);
            } catch (NamingException e) {
                e.printStackTrace();
            }
	    }
	}
}
